package com.deep.bilibili.controller;

import com.deep.bilibili.dao.FileMapper;
import com.deep.bilibili.dao.LiveDao;
import com.deep.bilibili.dto.FileDto;
import com.deep.bilibili.dto.LiveDto;
import com.deep.bilibili.entity.File;
import com.deep.bilibili.util.ResponseUtil;
import com.deep.bilibili.constant.SRSConstant;
import cn.hutool.core.date.DateUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.deep.bilibili.repository.LiveRepository;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RequestMapping("/api")
@RestController
public class ApiController {

    private static Logger logger = LoggerFactory.getLogger(HomeController.class);

    @Autowired
    private FileMapper fileMapper;

    
    @Autowired
    LiveRepository liveRepository;


    @PostMapping("/files")
    public Object files() {
        List<File> files = fileMapper.getFileList();
        List<FileDto> fileDtos = new ArrayList<>();
        for (File file : files) {
            FileDto fileDto = new FileDto();
            BeanUtils.copyProperties(file, fileDto);
            fileDtos.add(fileDto);
        }
        return ResponseUtil.ok(fileDtos);

    }

    @PostMapping("/file")
    public Object file(@Validated @RequestBody  FileDto fileDto) {
        logger.info("FileDto = {}", fileDto);
        File file = fileMapper.selectByPrimaryKey(fileDto.getId());
        BeanUtils.copyProperties(file, fileDto);
        return ResponseUtil.ok(fileDto);
    }


    

    //////////////// 接口 //////////////////////////

    /**
     * 直播列表(接口)
     * @return
     */
    @PostMapping("/lives")
    @ResponseBody
    public Object getList() {
        List<LiveDao> lives = liveRepository.findAll();
        List<LiveDto> liveDtos = new ArrayList<>();

        for(LiveDao liveDO: lives) {
            LiveDto liveDto = new LiveDto();
            liveDto.setId(liveDO.getId());
            liveDto.setPath(liveDO.getPath());
            liveDto.setLiveno(liveDO.getLiveno());
            liveDto.setRtcUrl(liveDO.getRtcUrl());
            liveDtos.add(liveDto);
        }
 
        return ResponseUtil.ok(liveDtos);

    }

    /**
     * 直播详情
     * @param id
     * @return
     */
    @PostMapping("/liveDetail")
    public Object getDetail(@RequestParam Long id) {
        LiveDao liveDO = liveRepository.getOne(id);
        LiveDto liveDto = new LiveDto();
        liveDto.setId(liveDO.getId());
        liveDto.setPath(liveDO.getPath());
        liveDto.setLiveno(liveDO.getLiveno());
        liveDto.setRtcUrl(liveDO.getRtcUrl());

        return ResponseUtil.ok(liveDto);
 
    }

    /**
     * 添加直播 接口
     * @param liveDto
     * @return
     */
    @PostMapping("/liveAdd")
    public Object addLive(@RequestBody LiveDto liveDto) {
        String path = liveDto.getPath();
        String liveno = liveDto.getLiveno();

        LiveDao liveDO = new LiveDao();
        liveDO.setUid(1L);
        liveDO.setPath(path);
        liveDO.setLiveno(liveno);

        String rtmpUrl = SRSConstant.RTMP_DOMAIN + path;
        String rtcUrl = SRSConstant.RTC_DOMAIN + path;

        liveDO.setRtcUrl(rtcUrl);
        liveDO.setRtmpUrl(rtmpUrl);
        liveDO.setDownUrl("");

        String datetime = DateUtil.format(new Date(), "yyyy/MM/dd HH:mm:ss");
        liveDO.setAddTime(datetime);
        liveDO.setUpdateTime(datetime);

        liveRepository.save(liveDO);

        return ResponseUtil.ok();
 
    }
}
